/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.addressBook.controller;

import com.addressBook.baseService.Search;
import com.addressBook.bean.UserInfo;
import com.addressBook.controller.UIController;
import com.fxexperience.javafx.animation.FlipOutYTransition;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.animation.Transition;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;

/**
 * FXML Controller class
 *
 * @author Lam
 */
public class SearchController implements Initializable {

    private static SearchController instance;

    public static SearchController getSearchController() {
        return instance;
    }

    @FXML
    ImageView searchBackBtn;
    @FXML
    TextField searchField;
    @FXML
    Label searchResult;
    @FXML
    ListView<Pane> resultList;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        instance = this;

        searchBackBtn.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UIController uc = UIController.getUIController();
                Transition trans = new FlipOutYTransition(uc.searchPanel);
                trans.setOnFinished(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent event) {
                        uc.searchPanel.setVisible(false);
                        uc.back2main();
                    }
                });
                trans.play();
            }
        });

        searchField.textProperty().addListener(new ChangeListener<String>() {

            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                Search search = new Search();
                searchResult.setText("");
                if (newValue.trim().length() <= 0) {
                    resultList.setItems(null);
                    return;
                }
                ArrayList<UserInfo> result = search.baseSearch(newValue.trim());
                ArrayList<Pane> resultPanes = new ArrayList<Pane>();
                for (UserInfo bean : result) {
                    Label nameLabel = new Label();
                    String s= bean.getN();
                    String[] s1=s.split(";");
                    nameLabel.setText(s1[0]+s1[1]);
                    Label telLabel = new Label();
                    s1=bean.getTEL().get(0).split(":");
                    
                    telLabel.setText(s1[1]);
                    telLabel.setLayoutX(230);
                    Pane newPane = new Pane();
                    newPane.getChildren().addAll(nameLabel, telLabel);
                    resultPanes.add(newPane);
                }
                ObservableList<Pane> items = FXCollections.observableArrayList(resultPanes);
                resultList.setItems(items);
                searchResult.setText("Result : " + resultPanes.size());
            }
        });
    }

}
